<?php
if ( !defined('IN_ADMINCP') || !defined('IN_SEC') ){
	exit('Access Denied.');
}

$cp = $_G['gp_cp'];
$call = conf('call-center');
if ( $cp ){
	$tmpname .= '_cp';
}
$where = array();

$type = isset($_G['gp_type']) && $_G['gp_type'] > 0 ? abs(intval($_G['gp_type'])) : -1;
$result = isset($_G['gp_result']) && $_G['gp_result'] > 0 ? abs(intval($_G['gp_result'])) : -1;

if( $type >= 0 ){
	$where['type'] = $type;
}
if( $result >= 0 ){
	$where['result'] = $result;
}

$name = $_G['gp_name'] ? htmlspecialchars($_G['gp_name']) : '';
$contact_name = $_G['gp_contact_name'] ? htmlspecialchars($_G['gp_contact_name']) : '';

if( $contact_name ){
	$where[] = "`phone` LIKE '%".DB::EscapeString($contact_name)."%'";
}

$begin_time = $_G['gp_begin_time'] ? strtotime($_G['gp_begin_time']) : 0;
$end_time = $_G['gp_end_time'] ? strtotime($_G['gp_end_time']) : 0;

if( $begin_time && $end_time ){
	$where[] = "`begin_time` >= {$begin_time} AND `end_time` <= {$end_time}";
}
$members = (limitquery('admincp_member', array('departmentid' => 3)));

$admins = array();
foreach( $members as $k => $v ){
	$admins[$v['id']] = array(
		'id' => $v['id'],
		'name' => heivrid($v)
	);
}

$adminid = $_G['gp_adminid'] > 0 ? (intval($_G['gp_adminid'])) : 0;
if( $adminid ){
	$where['adminid'] = $adminid;
}
$types = array(
	'未知', '来电', '去电'
);
$results = array(
	'未知', '呼入未接', '呼入拒接', '未接通', '接通'
);
$results_colors = array(
	'grey', '', '', 'red', 'green'
);

$channels = parse_array(limitquery('call_center'));

$count = DB::Count('call_center_log', $where);
$pages = pages($count, $pagesize ? $pagesize : 20);
$options = array('order' => 'ORDER BY `id` DESC');
$logs = limitquery('call_center_log', $where, $options, $pages['offset'], $pages['pagesize']);

$phones = idserialize($logs, 'phone');

$customers = parse_array(limitquery('customer', array("`contact_phone` IN('{$phones}')")), 'contact_phone');

function log_time($begin_time = 0, $end_time = 0){
	if( !$begin_time || !$end_time ){
		return '00:00:00';
	}
	$time = $end_time - $begin_time;
	$i = abs(intval($time / 60));
	$h = abs(intval($i / 60));
	$i -= ($h * 60);
	$s = abs(intval($time % 60));
	$time = sprintf('%02d:%02d:%02d', $h, $i, $s);
	return $time;
}

?>